home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’96 / Sessions ’96 / AI and The Mac / Demos / NeuroLab Demo v1.2 / Extensions / NeuroLab.h next >
Encoding:
Text File  |  1996-06-22  |  8.6 KB  |  154 lines  |  [TEXT/EXTE]

  1. ** NeuraLab™ -- Neural Network Library
  2. ** Copyright © 1994-95 by Mikuni Berkeley R&D Corp. All right reserved.
  3.  
  4. ** Version 1.0 -- May 15, 1994; by Song-Min Wang
  5. **         1.5 -- 1/15/95 (SM Wang)
  6.  
  7. constant NN_PASS_ARRAY_DIMENSION    is    8;
  8. constant INFO_DIMENSION                is    10;
  9.  
  10. double        _connArray[], _passedRealArray[];
  11. long        _sizeArray[], _passedLongArray[],
  12.             _dofOfPassedArray, _setOfPassedArray;
  13.  
  14.  
  15.  
  16. // ========================================= Calculate Sigmoid function or its derivative
  17. double Sigmoid(double xx, double TT, integer fd)
  18. {
  19.     double    fSigm;
  20.     
  21.     fSigm = 1.0/(1.0+exp(-xx/TT));
  22.     
  23.     if (fSigm < 0.01) fSigm = 0.01;
  24.     else if (fSigm > 0.99) fSigm = 0.99;
  25.     
  26.     if (fd)
  27.         return fSigm*(1.0-fSigm)/TT;  // Return the derivative when fd != 0
  28.     else
  29.         return fSigm;  // Return function value when fd == 0
  30. }
  31.  
  32. // ====================================== Calculate arctangent function or its derivative
  33. double Hypertan(double xx, double TT, double fd)
  34. {
  35.     double    f1, nnResult;
  36.     
  37.     f1 = exp(xx/TT);
  38.     nnResult = (f1-1.0/f1)/(f1+1.0/f1);
  39.     
  40.     if (nnResult < -0.99) nnResult = -0.99;
  41.     else if (nnResult > 0.99) nnResult = 0.99;
  42.     
  43.     if (fd)
  44.         return (1.0-nnResult*nnResult)/TT;
  45.     else
  46.         return nnResult;
  47. }
  48.  
  49. // =========================================== Calculate function value of Gauss function
  50. double Gauss(double xx, double Devi, integer fd)
  51. {
  52.     double    fG, pp;
  53.     
  54.     pp = xx/Devi;
  55.     fG = exp(-0.5*pp*pp)/(Devi*Sqrt(2.0*PI));
  56.     if (fd)
  57.         return -pp*fG/Devi;
  58.     else
  59.         return fG;
  60. }
  61.  
  62. // =================================================================== Minor error occurs
  63. procedure  ErrorMsg(string msg0)
  64. {
  65.     integer    bNum;
  66.     
  67.     bNum = MyBlockNumber();
  68.     if (msg0 == "")
  69.         UserError("Error in [" + BlockName(bNum) + "], BN: " + bNum + ".");
  70.     else
  71.         UserError(msg0 + " Error in [" + BlockName(bNum) + "], BN: " + bNum + ".");
  72. }
  73.  
  74. // ================================================================= Serious error occurs
  75. procedure  ErrorAbort(string msg0)
  76. {
  77.     ErrorMsg(msg0);
  78.     AbortAllSims();
  79. }
  80.  
  81. // ========================================================= Check for positive long data
  82. ** upperBound < 1.0 means no upper bound
  83. double ValueValidation(double xx, double upperBound)
  84. {
  85.     if (NoValue(xx) || xx < 1.0)
  86.         ErrorAbort("Invalid data setting: "+xx);
  87.     else if (upperBound > 1.0 && xx > upperBound)
  88.         ErrorAbort("The input data exceed the upper limit.");
  89.     else
  90.         return Floor(xx+0.5);
  91. }
  92.  
  93. // ======================================================================================
  94. procedure SetMyBlockLabel(Str255 lab)
  95. {
  96.     long    nB;
  97.     
  98.     nB = MyBlockNumber();
  99.     SetBlockLabel(nB, lab);
  100. }
  101.  
  102. //******************************** Functions that handle passing array by block connector
  103. // ============================== Extract information and array pointer from passed array
  104. // List of return values:  0 -- No error.\
  105. //                         1 -- The connector value is not a array
  106. //                        -1 -- This is a regular array
  107. //                         2 -- Wrong array dimension
  108.  
  109. //                         (Error codes 4, 8, 16, 32 can be combined)
  110. //                         4 -- Can't get size information of the real array
  111. //                         8 -- Wrong dimension of the size array
  112. //                        16 -- Can't get the passed real array
  113. //                        32 -- Array dimension and the size information are incompatible
  114. // 
  115. double ExtractRealArrayOld(double passed)
  116. {
  117.     double    nnResult;
  118.     long    ll, ii;
  119.     
  120.     nnResult = 0.0;
  121.     if (!GetPassedArray(passed, _connArray))
  122.         return 1.0;  //The passed data is not an array
  123.     if (!NoValue(_connArray[0]))
  124.         return -1.0;; //This is a regular array
  125.     if (GetDimension(_connArray) != 8)
  126.         return 2.0;
  127.     
  128.     if (!GetPassedArray(_connArray[2], _sizeArray))
  129.         nnResult += 4.0;
  130.     if (GetDimension(_sizeArray) != _connArray[1]+1)
  131.         nnResult += 8.0;
  132.  
  133.     if (!GetPassedArray(_connArray[3], _passedRealArray))
  134.         nnResult += 16.0;
  135.     ll = 1;
  136.     for (ii=0; ii<=_connArray[1]; ii++)
  137.         ll *= _sizeArray[ii];
  138.     if (GetDimension(_passedRealArray) != ll) {
  139.         nnResult += 32.0;
  140.         UserError("ll="+ll+"  "+GetDimension(_passedRealArray));
  141.     }
  142.     
  143.     if (nnResult == 0.0)
  144.         return _connArray[3];
  145.     else
  146.         return nnResult;
  147.  
  148.  
  149. }
  150.  
  151. // =================================================================================
  152. procedure CheckNNPassedArray(double passed)
  153. {
  154.     if (!GetPassedArray(passed, _connArray))
  155.         ErrorAbort("Can't get passed array.");
  156.     
  157.     if (!NoValue(_connArray[0]))
  158.         ErrorAbort("The passed array does not have a proper format.");
  159.     
  160.     if (GetDimension(_connArray) != NN_PASS_ARRAY_DIMENSION)
  161.         ErrorAbort("The passed array does not has a right dimension.");
  162. }
  163.  
  164. // =================================================================================
  165. long ExtractArrayDOF(double passed)
  166. {
  167.     long nnResult;
  168.     
  169.     GetPassedArray(passed, _connArray);
  170.     
  171.     nnResult = Floor(_connArray[1]+0.5);
  172.     if (nnResult < 1)
  173.         ErrorAbort("Invalid DOF of the passed arrays.");
  174.     else
  175.         return nnResult;
  176.  
  177.  
  178. }
  179.  
  180. // =================================================================================
  181. long ExtractSetNumber(double passed)
  182. {
  183.     long    nnResult;
  184.     
  185.     GetPassedArray(passed, _connArray);
  186.     
  187.  
  188.     nnResult = Floor(_connArray[2]+0.5);
  189.     if (nnResult < 1)
  190.         ErrorAbort("Invalid DOF of the passed arrays.");
  191.     else
  192.         return nnResult;
  193.  
  194.  
  195.  
  196. }
  197.  
  198. // =================================================================================
  199. long ExtractRowNumber(double passed, long setIndex)
  200. {
  201.     long    nnDOF, nnSet;
  202.     
  203.     GetPassedArray(passed, _connArray);
  204.     
  205.     if (setIndex < 0)
  206.         setIndex = 0;
  207.     nnDOF = ExtractArrayDOF(passed);
  208.  
  209.     nnSet = ExtractSetNumber(passed);
  210.     if (setIndex > nnSet-1)
  211.         ErrorAbort("The passed array has a invalid number of sets.");
  212.     else if (nnSet == 1 && nnDOF == 1)
  213.         return _connarray[3];
  214.     else {
  215.         GetPassedArray(_connArray[3], _sizeArray);
  216.         return _sizeArray[setIndex*nnDOF];
  217.  
  218.     }
  219. }
  220.  
  221. // =================================================================================
  222. long ExtractColNumber(double passed, long setIndex)
  223. {
  224.     long    nnDOF, nnSet;
  225.     
  226.     GetPassedArray(passed, _connArray);
  227.     
  228.  
  229.     if (setIndex < 0)
  230.         setIndex = 0;
  231.     nnDOF = ExtractArrayDOF(passed);
  232.     if (nnDOF == 1)
  233.         return 1;
  234.  
  235.     nnSet = ExtractSetNumber(passed);
  236.     if (setIndex > nnSet-1)
  237.         ErrorAbort("The passed array has a invalid number of sets.");
  238.     else {
  239.         GetPassedArray(_connArray[3], _sizeArray);
  240.         return _sizeArray[setIndex*nnDOF+1];
  241.  
  242.     }    
  243. }
  244.  
  245. // =================================================================================
  246. double ExtractSizeArray(double passed)
  247. {
  248.     GetPassedArray(passed, _connArray);
  249.     return _connArray[3];
  250. }
  251.  
  252. // =================================================================================
  253. ** setIndex = N :  The N-th passed array (the index starts from 0)
  254. **
  255. double ExtractRealArray(double passed)
  256. {
  257.     double    nnResult;
  258.     long    ll, ll1, ii, jj;
  259.     
  260.  
  261.     nnResult = 0.0;
  262.     GetPassedArray(passed, _connArray);
  263.     
  264.     _dofOfPassedArray = Floor(_connArray[1]+0.5);
  265.     if (_dofOfPassedArray < 1)
  266.         ErrorAbort("The passed array has an invalid DOF.");
  267.     
  268.     _setOfPassedArray = Floor(_connArray[2]+0.5);
  269.     if (_setOfPassedArray < 1)
  270.         ErrorAbort("The passed array has a unknown number of sets.");
  271.     
  272.     if (_setOfPassedArray == 1 && _dofOfPassedArray == 1) {
  273.         GetPassedArray(_connarray[4], _passedRealArray);
  274.         if (GetDimension(_passedRealArray) == _connArray[3])
  275.             return _connarray[4];
  276.         else
  277.             ErrorAbort("The passed array does not match its own dimension.");
  278.     }
  279.     else {
  280.         GetPassedArray(_connArray[4], _passedRealArray);
  281.         GetPassedArray(_connArray[3], _sizeArray);
  282.         ll = 0;
  283.         for (ii=0; ii<_connArray[2]; ii++) {
  284.             ll1 = 1;
  285.             for (jj=0; jj<_connArray[1]; jj++)
  286.                 ll1 *= _sizeArray[ii*_connArray[1]+jj];
  287.             ll += ll1;
  288.         }
  289.         if (GetDimension(_passedRealArray) == ll)
  290.             return _connArray[4];
  291.         else
  292.             ErrorAbort("The passed arrays do not match their own dimensions (" +
  293.                 ll + " vs " + GetDimension(_passedRealArray) + ").");
  294.     }
  295.  
  296.  
  297. }
  298.  
  299. // ----------------------------------------------------------------------------------
  300. double ExtractInfo1Array(double passed)
  301. {
  302.     CheckNNPassedArray(passed);
  303.  
  304.     
  305.     return _connArray[5];
  306.  
  307.  
  308. }
  309.  
  310. // ==================================================================================
  311. double ExtractLongArray(double passed)
  312. {
  313.     double    nnResult;
  314.     
  315.     nnResult = 0.0;
  316.     if (!GetPassedArray(passed, _connArray))
  317.         return 1.0;  //Can't get passed array
  318.     if (!NoValue(_connArray[0]))
  319.         return -1.0;; //This is a regular array
  320.     if (_connArray[1] != 1 || GetDimension(_connArray) != 8)
  321.         return 2.0;
  322.     
  323.     if (!GetPassedArray(_connArray[2], _sizeArray))
  324.         nnResult += 4.0;
  325.     if (GetDimension(_sizeArray) != 2)
  326.         nnResult += 8.0;
  327.  
  328.     if (!GetPassedArray(_connArray[3], _passedLongArray))
  329.         nnResult += 16.0;
  330.     if (GetDimension(_passedLongArray) != _sizeArray[0]*_sizeArray[1])
  331.         nnResult += 32.0;
  332.     
  333.     if (nnResult == 0.0)
  334.         return _connArray[3];
  335.     else
  336.         return nnResult;
  337.  
  338.  
  339. }
  340.